package pl.ptemich.eav.sprites.impl;

import java.util.Set;

import org.newdawn.slick.SlickException;

import pl.ptemich.eav.engine.Point;
import pl.ptemich.eav.engine.physics.DynamicParameters;
import pl.ptemich.eav.engine.physics.InteractiveObject;
import pl.ptemich.eav.engine.physics.ShapeParameters;
import pl.ptemich.eav.engine.physics.Updatable;
import pl.ptemich.eav.engine.physics.impl.DynamicParametersDefault;
import pl.ptemich.eav.engine.physics.impl.InteractiveObjectImpl;
import pl.ptemich.eav.engine.physics.impl.RectangleParameters;
import pl.ptemich.eav.sprites.AbstractStaticTile;

public class Hero extends AbstractStaticTile implements Updatable,
    InteractiveObject {

  private InteractiveObject physics;
  
  public Hero(float posX, float posY) throws SlickException {
    super(posX, posY, "resources/images/bugWhite.png");
  }

  @Override
  protected InteractiveObjectImpl createObjectPhysic(Point worldPosition, int width, int height) {
    DynamicParameters dynamicParameters = new DynamicParametersDefault();
    ShapeParameters shapeParams = new RectangleParameters(width, height);
    InteractiveObjectImpl phys = null;
    try {
      phys = new InteractiveObjectImpl(worldPosition, width / 2, height / 2, shapeParams, dynamicParameters);
      physics = phys;
    } catch (SlickException e) {
      e.printStackTrace();
    }
    return phys;
  }  
  
  @Override
  public void update(int delta) {
    physics.update(delta);
  }

  public void checkColission(Set<AbstractStaticTile> obstacles) {
    for (AbstractStaticTile tile : obstacles) {
      if (getStaticPhysic().checkCollision(tile.getStaticPhysic())) {
        transferTo(getStaticPhysic().getWorldPosition().getPosX(), tile.getStaticPhysic().getBoxU() + getStaticPhysic().getMarginY());
      }
    }
  }
  
  @Override
  public void doubleJump() {
    physics.doubleJump();
  }

  @Override
  public void jump() {
    physics.jump();
  }

  @Override
  public void moveLeft() {
    physics.moveLeft();
  }

  @Override
  public void moveRight() {
    physics.moveRight();
  }

  @Override
  public void stop() {
    physics.stop();
  }

  @Override
  public void transferTo(float posX, float posY) {
    physics.transferTo(posX, posY);
  }
}
